package Commands;

import java.io.File;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.*;

/**
 *  Created with IntelliJ IDEA.
 *  Authors: Henrique Alberto Rusa and Danilo Barbosa Porto
 *  Date: 01/12/13
 *
 *  This class implements the idea of our instructions.
 *  First, it is a simple string, but it will have sub strings where each one correspond to a certain parameter or
 *  function needed to the application we will be designing.
 */
public class InstructionPack implements Serializable, Command {

    /*
     *  This string will contain the filePath of the file we are considering on the instruction or it will be a null
     *  object just because it is not on use.
     */
    private File filePath;

    /*
     *  The integer CommandCode will have the code to compare instructionPacks on the remote node that received one of
     *  it´s objects.
     *  The reference of each code is on the Command Interface.
     */
    private int CommandCode;

    /*
     *  The Date type will be used to inform the remote nodes that receive this package of the last modified dat of the
     *  files used on this filesystem implementation. This parameter has update reasons of usage.
     */
    private Date lastModified;

    /*
     *  This table will be used to update nodes and synchronize the central server on the start of the application.
     */
    private Map<File,InetAddress> table;

    /*
     *  Parameter that enables instructions receivers to send information to the address pointed by this instance.
     */
    private InetAddress requestHost;

    /*
     * Getter to get the lastModified from the Instruction
     */
    public Date getLastModified() {
        return lastModified;
    }

    /*
     * Getter to get the FilePath from the Instruction
     */
    public File getFilePath() {
        return filePath;
    }

    /*
     * Getter to get the table from the Instruction
     */
    public Map<File, InetAddress> getTable() {
        return table;
    }

    /*
     * Getter to get the commandCode from the Instruction
     */
    public int getCommandCode() {
        return CommandCode;
    }

    /*
     * Getter to get the InetAddress of the host from the Instruction
     */
    public InetAddress getRequestHost(){
        return requestHost;
    }

    /*
     * Getter to get the FileName from the Instruction
     */
    public String getFileName(){
        String name = filePath.getName();
        return name;
    }

    /*
     *  Method that turn our instruction object into a Read Instruction to be sent over a connection. This call
     *  receives the filename to be read.
     */
    public void makeReadInstruction(File filePath){
        this.filePath = filePath;
        this.CommandCode = Command.read;
        this.lastModified = null;
        this.table = null;
    }

    /*
     *  Method that turn our instruction object into a Read Instruction to be sent over a connection. This call
     *  receives the filename to be read.
     */
    public void makeCreateInstruction(File filePath, Map<File,InetAddress> table){
        this.filePath = filePath;
        this.CommandCode = Command.read;
        this.lastModified = null;
        this.table = table;
    }

    /*
     *  Method that turn our instruction object into a Delete Instruction to be sent over a connection. This call
     *  receives the filename to be deleted.
     */
    public void makeDeleteInstruction(File filePath){
        this.filePath = filePath;
        this.CommandCode = Command.delete;
        this.lastModified = null;
        this.table = null;
    }

    /*
     *  Method that turn our instruction object into a exchange Instruction to be sent over a connection. This call
     *  receives the filename to be exchanged.
     */
    public void makeExchangeInstruction(File filePath){
        this.filePath = filePath;
        this.CommandCode = Command.exchange;
        this.lastModified = null;
        this.table = null;
    }

    /*
     *  Method that turn our instruction object into a update table Instruction to be sent over a connection. This call
     *  receives the hash from the central server to be copied.
     */
    public void makeUpdateTableInstruction(Map<File,InetAddress> table){
        this.filePath = null;
        this.CommandCode = Command.update;
        this.lastModified = null;
        this.table = table;
    }

    /*
     *  Method that turn our instruction object into a end connection Instruction to be sent over a connection.
     */
    public void makeEndConnectionInstruction(){
        this.CommandCode = Command.end;
    }

    /*
     *  Method to identify if the instruction package is a create instruction returning a boolean to the caller.
     */
    public boolean isCreateInstruction(){
        return this.CommandCode == Command.create;
    }

    /*
     *  Method to identify if the instruction package is a read instruction returning a boolean to the caller.
     */
    public boolean isReadInstruction(){
        return this.CommandCode == Command.read;
    }

    /*
     *  Method to identify if the instruction package is a delete instruction returning a boolean to the caller.
     */
    public boolean isDeleteInstruction(){
        return this.CommandCode == Command.delete;
    }

    /*
     *  Method to identify if the instruction package is a exchange instruction returning a boolean to the caller.
     */
    public boolean isExchangeInstruction(){
        return this.CommandCode == Command.exchange;
    }

    /*
     *  Method to identify if the instruction package is a update table instruction returning a boolean to the caller.
     */
    public boolean isUpdateTableInstruction(){
        return this.CommandCode == Command.update;
    }

    /*
     *  Method to identify if the instruction package is a end of connection instruction returning a boolean to the caller.
     */
    public boolean isEndConnectionInstruction(){
        return this.CommandCode == Command.end;
    }
}






































